#!/bin/bash

# Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
# Copyright (C) 2006-2010 RobotCub Consortium
# All rights reserved.
#
# This software may be modified and distributed under the terms of the
# BSD-3-Clause license. See the accompanying LICENSE file for details.

echo "Puts license text in order."
echo "Make sure you run this as ./scripts/admin/update-license"

# reset change log
rm -f license-*.txt copyright-*.txt

rm -rf ./license_check
mkdir ./license_check
git archive HEAD | tar -x -C ./license_check

for f in `cd license_check; find . -type f -iname "*.cpp" \
                                       -or -iname "*.h" \
                                       -or -iname "*.cmake" \
                                       -or -name "CMakeLists.txt" \
                | grep -v "/extern" \
                | grep -v "/qtquick2applicationviewer/" \
                | grep -v "/src_gen/" \
`; do
    ./scripts/admin/update-license-single $f
done


# license-bsd3.txt has files under BSD-3-Clause
# license-bsd2.txt has files under BSD-2-Clause
# license-bsd4.txt has files under BSD-4-Clause
# license-lgpl2.1+ has files under LGPL2.1+
# license-gpl2+.txt has files under GPL2+
# license-gpl3+.txt has files under GPL3+
# license-odd.txt has everything else

if [ -f license-bsd3.txt ]; then
  cat license-bsd3.txt  | grep "Copyright:" > copyright-bsd3.txt
  cat license-bsd3.txt >> license-good.txt
fi

if [ -f license-bsd2.txt ]; then
  cat license-bsd2.txt  | grep "Copyright:" > copyright-bsd2.txt
  cat license-bsd2.txt >> license-good.txt
fi


if [ -f license-bsd4.txt ]; then
  cat license-bsd4.txt  | grep "Copyright:" > copyright-bsd4.txt
  cat license-bsd4.txt >> license-good.txt
fi

if [ -f license-lgpl2.1+.txt ]; then
  cat license-lgpl2.1+.txt  | grep "Copyright:" > copyright-lgpl2.1+.txt
  cat license-lgpl2.1+.txt >> license-good.txt
fi

if [ -f license-gpl2+.txt ]; then
  cat license-gpl2+.txt  | grep "Copyright:" > copyright-gpl2+.txt
  cat license-gpl2+.txt >> license-good.txt
fi

if [ -f license-gpl3+.txt ]; then
  cat license-gpl3+.txt  | grep "Copyright:" > copyright-gpl3+.txt
  cat license-gpl3+.txt >> license-good.txt
fi

cat license-good.txt  | grep "Copyright:" > copyright-good.txt


(cat copyright-good.txt | sed "s/.*[0-9][0-9][0-9][0-9] */ , /g" | sed "s/ - / , /g" | sed "s/^[ ,]\+//g" | grep -i "[a-z]") > license-people.txt
(cat copyright-good.txt | sed "s/[^-0-9,]//g" | sed "s/[-,]$//g" | sed "s/,/\n/g" | sort -u | sed "s/^\([0-9]\+\)$/echo \1/g" | sed "s/\([0-9]\+\)-\([0-9]\+\)/seq \1 \2/g" | sed '/^\s*$/d'| sed "s/$/;/g" | sh | sort -u) > license-years.txt

# Construct license report
(

cat<<EOF
Unless otherwise stated, files in YARP are:
  Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
  All rights reserved.

  This software may be modified and distributed under the terms of the
  BSD-3-Clause license. See the accompanying LICENSE file for details.
EOF

cat<<EOF

Most YARP components are released under the terms of the BSD-3-Clause.
Some optional components are released under the terms of the LGPL-2.1 or later,
GPL-2.0 or later, GPL-3.0 or later, or Apache-2.0 License:
 * Some components are released under the terms of the LGPL license, version 2.1
   or later.
   + devices:
     - \`audioPlayerWrapper\`
     - \`audioRecorderWrapper\`
     - \`batteryClient\`
     - \`batteryWrapper\`
     - \`depthCamera\`
     - \`fakeLocalizerDevice\`
     - \`fakeNavigationDevice\`
     - \`imuBosch_BNO055\`
     - \`laserFromDepth\`
     - \`laserHokuyo\`
     - \`localization2DClient\`
     - \`map2DClient\`
     - \`map2DServer\`
     - \`navigation2DClient\`
     - \`opencv\`
     - \`ovrheadset\`
     - \`portaudio\`
     - \`portaudioPlayer\`
     - \`portaudioRecorder\`
     - \`Rangefinder2DClient\`
     - \`Rangefinder2DWrapper\`
     - \`rpLidar\`
     - \`rpLidar2\`
     - \`SDLJoypad\`
     - \`transformClient\`
     - \`transformServer\`
     - \`usbCamera\`
   + tools:
     - \`yarplogger-console\`
     - \`yarpmanager-console\`
     - \`yarprobotinterface\`
   + GUIs:
     - \`yarpbatterygui\`
     - \`yarpdataplayer\`
     - \`yarplaserscannergui\`
     - \`yarplogger\`
     - \`yarpmanager\`
     - \`yarpmotorgui\`
     - \`QtYARPView\` and \`yarpview\`
 * Some optional components use 3rd party libraries that may have some extra
   requirements.
   + \`YARP_robottestingframework\` and the Robot Testing Framework plugins use
     [Robot Testing Framework](https://github.com/robotology/robot-testing-framework/blob/master/LICENSE).
   + The \`xmlrpc\` carrier uses a custom version of the
     [xmlrpcpp](https://sourceforge.net/projects/xmlrpcpp/) library.
   + The \`bayer\` carrier uses alternatively
     [libdc1394](http://damien.douxchamps.net/ieee1394/libdc1394/) or, if the
     library is not available, links statically a few files taken from
     [libdc1394](http://libdc1394.git.sourceforge.net/git/gitweb.cgi?p=libdc1394/libdc1394;a=blob_plain;f=libdc1394/dc1394/bayer.c;hb=HEAD).
   + The \`h264\` carrier uses glib (LGPLv2.1 or later) and gstreamer (LGPLv2.1 or
     later).
   + The \`mpi\` carrier uses MPI (license dependent on the implementation).
   + The \`dynamixelAX12Ftdi\` device uses
     [libftdi](https://www.intra2net.com/en/developer/libftdi/).
   + The \`SDLJoypad\` device uses alternatively
     [SDL 1.2](https://libsdl.org/license.php).
   + The ffmpeg_grabber and ffmpeg_writer devices use
     [Ffmpeg](https://github.com/FFmpeg/FFmpeg/blob/master/LICENSE.md).
   + The ovrheadset device use the
     [Oculis SDK](https://developer.oculus.com/licenses/).
 * A few optional parts of YARP are released under the terms of the GPL license,
   version 2 or later:
   + \`YARP_gsl\` uses the GNU Scientific Library, under the GPL 2 or later.
   + \`example/matrix/gsl_example.cpp\` uses GSL, under the GPL 2 or later.
 * A few optional parts of YARP are released under the terms of the GPL license,
   version 3 or later:
   + \`QtYARPScope\` uses QCustomPlot that is GPL3+ and it is therefore GPL3+.
   + \`yarpscope\` uses \`QtYARPScope\` that is GPL3+ and it is therefore GPL3+.
   + \`yarpviz\` uses qgv that is GPL3+ and it is therefore GPL3+.
   + \`example/ContainerExample\` uses \`QtYARPScope\` and it is therefore GPL3+.
 * The \`yarpidl_thrift\` tool is released under the Apache-2.0 License.
EOF

cat<<EOF

The list of committers in the YARP repository (ordered by number of commit) is:
EOF
git shortlog -esn --no-merges | awk 'BEGIN{FS="\t"}{print "  " $2}' | sed "s/ <.\+>//"
cat<<EOF

The list of copyright holders for YARP is:
EOF

echo -n "  Copyright (C) "
echo -n `cat license-years.txt` | sed 's/ /, /g'
echo ""
( cat license-people.txt | sort | uniq -c | sort -k 1rn -k 2d | sed "s/ *\([0-9]*\) /(\1 files) /" ) | sed "s/^/  /" | sed "s/(1 files)/(1 file)/" | sed "s/\(.*\)\((.*)\) \(.*\)/  \1\3 \2/"
echo ""

cat<<EOF
The identifier "RobotCub Consortium" used in some copyright statements is
equivalent to the following list of institutions:
  * Ecole Polytechnique Federale de Lausanne - Biologically-Inspired
    Robotics Group (BIRG) and Learning Algorithms and Systems Lab (LASA),
    Switzerland
  * IST Lisbon - Computer Vision and Robotics Lab Lisbon - Portugal
  * Italian Institute of Technology - Dept. of Robotics, Brain and
    Cognitive Sciences - Genova, Italy
  * Sant'Anna School of Advanced Studies - ARTS Lab - Pisa - Italy
  * Telerobot S.r.l. - Genova - Italy
  * University of Ferrara - Department of Biomedical Science - Human
    Physiology - Ferrara - Italy
  * University of Genova - LIRA-Lab, Dipartimento di Informatica,
    Sistemistica e Telematica - Genova - Italy
  * University of Hertfordshire - School of Computer Science - United
    Kingdom
  * University of Uppsala - Department of Psychology - Uppsala - Sweden
  * University of Zurich - Artificial Intelligence Lab, Department of
    Information Technology - Zurich - Switzerland
  * [2005-2008] University of Salford - Centre for Robotics and
    Automation - Salford - United Kingdom
  * [2009-2010] The University of Sheffield, Dept. of Automatic Control &
    Systems Engineering, Sheffield, UK
EOF

if [ -f license-odd.txt ]; then
  cat<<EOF

The following files licenses are odd, or unknown:

EOF

  cat license-odd.txt | sed "s|^\./||"
fi


) > COPYING

rm -Rf copyright-*.txt license-*.txt license_check

echo
echo "COPYING file updated"
echo
